package com.ruoyi.report_gc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_gc.domain.bo.GcsyjcbgBsDetailBo;
import com.ruoyi.report_gc.domain.vo.GcsyjcbgBsDetailVo;
import com.ruoyi.report_gc.domain.GcsyjcbgBsDetail;
import com.ruoyi.report_gc.mapper.GcsyjcbgBsDetailMapper;
import com.ruoyi.report_gc.service.IGcsyjcbgBsDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 钢材试验检测报告详情Service业务层处理
 *
 * @author llb
 * @date 2024-06-21
 */
@RequiredArgsConstructor
@Service
public class GcsyjcbgBsDetailServiceImpl implements IGcsyjcbgBsDetailService {

    private final GcsyjcbgBsDetailMapper baseMapper;

    /**
     * 查询钢材试验检测报告详情
     */
    @Override
    public GcsyjcbgBsDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询钢材试验检测报告详情列表
     */
    @Override
    public TableDataInfo<GcsyjcbgBsDetailVo> queryPageList(GcsyjcbgBsDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<GcsyjcbgBsDetail> lqw = buildQueryWrapper(bo);
        Page<GcsyjcbgBsDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询钢材试验检测报告详情列表
     */
    @Override
    public List<GcsyjcbgBsDetailVo> queryList(GcsyjcbgBsDetailBo bo) {
        LambdaQueryWrapper<GcsyjcbgBsDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<GcsyjcbgBsDetail> buildQueryWrapper(GcsyjcbgBsDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<GcsyjcbgBsDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, GcsyjcbgBsDetail::getExcelId, bo.getExcelId());
        lqw.eq(StringUtils.isNotBlank(bo.getGongczj()), GcsyjcbgBsDetail::getGongczj, bo.getGongczj());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcjmj()), GcsyjcbgBsDetail::getGongcjmj, bo.getGongcjmj());
        lqw.eq(StringUtils.isNotBlank(bo.getQvfqdjszb()), GcsyjcbgBsDetail::getQvfqdjszb, bo.getQvfqdjszb());
        lqw.eq(StringUtils.isNotBlank(bo.getQvfqdjcjg()), GcsyjcbgBsDetail::getQvfqdjcjg, bo.getQvfqdjcjg());
        lqw.eq(StringUtils.isNotBlank(bo.getKanglqdjszb()), GcsyjcbgBsDetail::getKanglqdjszb, bo.getKanglqdjszb());
        lqw.eq(StringUtils.isNotBlank(bo.getKanglqdjcjg()), GcsyjcbgBsDetail::getKanglqdjcjg, bo.getKanglqdjcjg());
        lqw.eq(StringUtils.isNotBlank(bo.getDuanhscljszb()), GcsyjcbgBsDetail::getDuanhscljszb, bo.getDuanhscljszb());
        lqw.eq(StringUtils.isNotBlank(bo.getDuanhscljcjg()), GcsyjcbgBsDetail::getDuanhscljcjg, bo.getDuanhscljcjg());
        lqw.eq(StringUtils.isNotBlank(bo.getZuidlzysljszb()), GcsyjcbgBsDetail::getZuidlzysljszb, bo.getZuidlzysljszb());
        lqw.eq(StringUtils.isNotBlank(bo.getZuidlzysljcjg()), GcsyjcbgBsDetail::getZuidlzysljcjg, bo.getZuidlzysljcjg());
        lqw.eq(StringUtils.isNotBlank(bo.getQiangqbjszb()), GcsyjcbgBsDetail::getQiangqbjszb, bo.getQiangqbjszb());
        lqw.eq(StringUtils.isNotBlank(bo.getQiangqbjcjg()), GcsyjcbgBsDetail::getQiangqbjcjg, bo.getQiangqbjcjg());
        lqw.eq(StringUtils.isNotBlank(bo.getQvqbjszb()), GcsyjcbgBsDetail::getQvqbjszb, bo.getQvqbjszb());
        lqw.eq(StringUtils.isNotBlank(bo.getQvqbjcjg()), GcsyjcbgBsDetail::getQvqbjcjg, bo.getQvqbjcjg());
        lqw.eq(StringUtils.isNotBlank(bo.getDaunltz()), GcsyjcbgBsDetail::getDaunltz, bo.getDaunltz());
        lqw.eq(StringUtils.isNotBlank(bo.getWanqxnjcjg()), GcsyjcbgBsDetail::getWanqxnjcjg, bo.getWanqxnjcjg());
        lqw.eq(StringUtils.isNotBlank(bo.getFanxwqxnjcjg()), GcsyjcbgBsDetail::getFanxwqxnjcjg, bo.getFanxwqxnjcjg());
        lqw.eq(StringUtils.isNotBlank(bo.getZhonglpcjszb()), GcsyjcbgBsDetail::getZhonglpcjszb, bo.getZhonglpcjszb());
        lqw.eq(StringUtils.isNotBlank(bo.getZhonglpcjcjg()), GcsyjcbgBsDetail::getZhonglpcjcjg, bo.getZhonglpcjcjg());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjg()), GcsyjcbgBsDetail::getShiyjg, bo.getShiyjg());
        return lqw;
    }

    /**
     * 新增钢材试验检测报告详情
     */
    @Override
    public Boolean insertByBo(GcsyjcbgBsDetailBo bo) {
        GcsyjcbgBsDetail add = BeanUtil.toBean(bo, GcsyjcbgBsDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改钢材试验检测报告详情
     */
    @Override
    public Boolean updateByBo(GcsyjcbgBsDetailBo bo) {
        GcsyjcbgBsDetail update = BeanUtil.toBean(bo, GcsyjcbgBsDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(GcsyjcbgBsDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除钢材试验检测报告详情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
